package com.runjian.rundo.iot.agent.business.kafka;

import org.springframework.kafka.core.KafkaTemplate;

import com.alibaba.fastjson2.JSON;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class KafkaSender {
    private final KafkaTemplate<String, String> kafkaTemplate;

    /**
     * 构造器方式注入 Spring Kafka Template
     *
     * @param kafkaTemplate Spring Kafka Template
     */
    public KafkaSender(KafkaTemplate<String, String> kafkaTemplate) {
        this.kafkaTemplate = kafkaTemplate;
    }

    public void send(String topic, String data) {
        // 对 topic = hello2 的发送消息
        kafkaTemplate.send(topic, data);
    }

    public void send(String topic, Object obj) {
        String data = JSON.toJSONString(obj);
        // 对 topic = hello2 的发送消息
        kafkaTemplate.send(topic, data);
    }
}
